<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">

          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" @click="$refs.addForm.add()" icon="plus">新增</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.code"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >

      <span slot="action" slot-scope="text, record">
        <a @click="$refs.editForm.edit(record)">编辑</a>
        <a-divider type="vertical"/>
        <a-dropdown>
          <a class="ant-dropdown-link"> 更多 <a-icon type="down"/></a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
                <a>删除</a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </span>
    </s-table>

    <add-form ref="addForm" @ok="handleOk"/>
    <edit-form ref="editForm" @ok="handleOk"/>

  </a-card>
</template>

<script>
  import {STable} from '@/components'
  import {page, del} from '@/api/modular/erComment/erCommentManage'
  import addForm from './erCommentAddForm'
  import editForm from './erCommentEditForm'

  export default {
    components: {
      STable,
      addForm,
      editForm
    },

    data() {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {},  // 查询参数
        columns: [       // 表头
          {
            title: '主键id',
            dataIndex: 'id'
          },
          {
            title: '评论内容',
            dataIndex: 'comment'
          },
          {
            title: '评论人头像',
            dataIndex: 'avatar'
          },
          {
            title: '评论者昵称',
            dataIndex: 'nickName'
          },
          {
            title: '帖子id',
            dataIndex: 'eid'
          },
          {
            title: '评论点赞数',
            dataIndex: 'love'
          },
          {
            title: '评论时间',
            dataIndex: 'createTime'
          },
          {
            title: '评论人id',
            dataIndex: 'createUser'
          },
          {
            title: '更新时间',
            dataIndex: 'updateTime'
          },
          {
            title: '更新人',
            dataIndex: 'updateUser'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: {customRender: 'action'}
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id',
        }
      }
    },

    methods: {
      del(record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      },
      handleOk() {
        this.$refs.table.refresh()
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
